/*******************************************************************************
 * Copyright 2009 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/**
 * Place holder for copyright, needs to be replaced before release.
 */
package com.softao.ming.gwt.core.internal.runtime.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.softao.ming.gwt.core.runtime.client.application.IConfigurationElement;
import com.softao.ming.gwt.core.runtime.client.application.IExtension;
import com.softao.ming.gwt.core.runtime.client.application.IExtensionPoint;

/**
 * 
 */
public class Extension implements IExtension {
	private final IExtensionPoint mExtensionPoint;
	private final String mUniqueIdentifier;
	private final IConfigurationElement[] mConfigurationElements;
	
	/**
	 * @param extensionPoint 
	 * @param extensionElement 
	 */
	public Extension(IExtensionPoint extensionPoint, Element extensionElement) {
		mExtensionPoint = extensionPoint;
		mUniqueIdentifier = extensionElement.getAttribute("id");
		NodeList childNodes = extensionElement.getChildNodes();
		if (childNodes != null && childNodes.getLength() > 0) {
			List<IConfigurationElement> list = new ArrayList<IConfigurationElement>();
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node childNode = childNodes.item(i);
				if (childNode instanceof Element) {
					Element ele = (Element)childNode;
					list.add(new ConfigurationElement(this, ele));
				}
			}
			mConfigurationElements = list.toArray(new IConfigurationElement[list.size()]);
		} else {
			mConfigurationElements = null;
		}
	}
	
	@Override
	public IConfigurationElement[] getConfigurationElements() {
		return mConfigurationElements;
	}

	@Override
	public IExtensionPoint getExtensionPoint() {
		return mExtensionPoint;
	}

	@Override
	public String getUniqueIdentifier() {
		return mUniqueIdentifier;
	}

}
